<script setup lang="ts">
import { delAct, getActList } from "@/api/admin";
import { router } from "@/router/index";
import { message } from "@/utils/message";
import { ElMessageBox } from "element-plus";
import { onMounted, ref } from "vue";

defineOptions({
  name: "ActivityList"
});
const actList = ref([]);

const toPage = page => {
  router.push(page);
};

const loadActList = async () => {
  let params = {
    pageNum: 1,
    pageSize: 500
  };
  let { data } = await getActList(params);
  actList.value = data.records || [];
};

async function handleDelete(row) {
  ElMessageBox.confirm(
    `是否确认要<strong>删除 </strong><strong style='color:var(--el-color-primary)'>${
      row.name
    }</strong> ?`,
    "系统提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
      dangerouslyUseHTMLString: true,
      draggable: true
    }
  ).then(async () => {
    const res = await delAct(row.id);
    if (res.code != 200) return;
    message(`删除成功`, { type: "success" });
    loadActList();
  });
}

onMounted(() => {
  loadActList();
});
</script>
<template>
  <div>
    <div class="activity">
      <div class="content">
        <div class="header">
          <div>活動一覽</div>
          <div class="btns">
            <div class="btn" @click="toPage('/activityAdd')">新增活動</div>
          </div>
        </div>
        <div class="detail">
          <div class="items">
            <div
              v-for="item in actList"
              :key="item"
              class="item"
              @click="toPage('/activityEdit?id=' + item.id)"
            >
              <div class="name block">
                {{ item.name || "--" }}
              </div>
              <img
                src="@/assets/del.png"
                class="img"
                @click.stop="handleDelete(item)"
              />
            </div>
            <el-empty v-if="actList.length == 0" description="暂无数据~" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.activity {
  width: 80%;
  height: calc(100vh - 230px);
  display: flex;
  border-radius: 0 25px 25px 0;

  .block::before {
    content: "";
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    margin-right: 5px;
    background-color: #9dbdba;
  }
  .block::after {
    content: "";
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    margin-left: 5px;
    background-color: #9dbdba;
  }
  .content {
    flex: 1;
    border: 1px solid #d9d9d9;
    border-radius: 25px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background-color: rgba(255, 255, 255, 0.9);
    .header {
      font-size: 25px;
      font-weight: bold;
      height: 100%;
      height: 102px;
      padding: 30px;
      background-color: #9dbdbab2;
      box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .btns {
      color: #9dbdba;
      font-size: 20px;
      .btn {
        width: 190px;
        height: 62px;
        border-radius: 5px;
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      }
    }
    .detail {
      flex: 1;
      padding: 20px;
      overflow-x: auto;
      .items {
        color: #9dbdba;
        font-size: 18px;
        font-weight: bold;
        overflow: auto;
        border-radius: 10px;
        .item {
          display: flex;
          align-items: center;
          margin-bottom: 15px;
        }
        .name {
          width: 100%;
          min-height: 62px;
          padding: 10px 15px;
          border: 2px solid #9dbdba;
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          margin-right: 15px;
        }
        .img {
          width: 62px;
          height: 62px;
          cursor: pointer;
        }
        .item.act {
          color: #409eff;
          border-color: #409eff;
        }
        .item.act::before,
        .item.act::after {
          background-color: #409eff;
        }
      }
    }
  }
}
</style>
